<?php
namespace Admin\Controller;
use Think\Controller;
class CustomerController extends AuthController {
    
    public function _initialize(){
        parent::_initialize();
    }

    public function index(){
	    $customer   = M("customer");
		import("ORG.Util.Page");// 导入分页类
		$count    = $customer->count();
		$page     = new \Think\Page($count,20);//tp3.2新增
		$show     = $page->show();
		$limit    = $page->firstRow.','.$page->listRows;
		
		$list     = $customer -> order("listorder desc, cid desc") 
				              -> limit($limit)->select();
		$now      = time();
		$this->assign(array(
		    'list'   =>  $list,
			'page'   =>  $show,
			'now'    =>  $now,
		));
        $this->display();
    }
	
	/*新增/修改客户信息*/
	public function customer_update(){
	    $cid        =   I('get.cid');
	    $customer   =   M("customer"); 
		
		if (IS_POST){
			 $data['cuname']      =  trim(I('post.cuname','','htmlspecialchars'));
			 $data['cphone']       =  trim(I('post.cphone','','htmlspecialchars'));
			 $data['cwechat']      =  trim(I('post.cwechat','','htmlspecialchars'));
			 $data['cqq']          =  trim(I('post.cqq','','htmlspecialchars'));
			 $data['ctype']        =  I('post.ctype') ? trim(I('post.ctype','','htmlspecialchars')) : "1";
			 $data['status']       =  I('post.status') ? trim(I('post.status','','htmlspecialchars')) : "1";
			 $data['content']      =  trim(I('post.content','','htmlspecialchars'));
			 $data['inputtime']    =  I("post.inpuuttime") ? strtotime(trim(I('post.inputtime','','htmlspecialchars'))) : time();
			 $data['listorder']    =  I('post.listorder') ? trim(I('post.listorder')): 0;
		}
		
	    if (!isset($cid) || $cid ==""){//id为空表示新增
	          $count = $customer -> add($data);
			  if($count){
				  $this->success("添加成功",U('Customer/index'));
			  }else{
				  $this->error("添加失败");	
			  }
		}else{//修改信息
		      $where['cid'] = $cid;
	          $count = $customer->where($where) -> save($data);
			  if($count){
				  $this->success("修改成功",U('Customer/index'));
			  }else{
				  $this->error("修改失败");	
			  }
		}
	}
	
	/*客户资料编辑*/
	public function customer_edit(){
	    $cid      = I('get.cid');
		$customer = M("customer");
		$where    = array('cid' => $cid);
		if (!empty($cid)){
		     $arr = $customer -> where('cid = '.$cid.'') 
						      -> order('cid desc') 
						      -> find();
		}else{
		    $this->error("参数传递错误");
	    }
		 
		$this->assign(array(
		    'data' => $arr,
		));
        $this->display();
	}
	
	
	/*ajax获得客户联系方式*/
	public function ajaxget_cus(){
	    $cid      = I('get.cid');
		$customer = M('customer');
		$data  = "";
		if($cid =="" || $cid  == "0"){
		   $data = "";
		}else{
		    $where['cid'] = $cid;
			$data = $customer -> where($where) -> find();
			$data = json_encode($data);
		}
		$this->ajaxReturn($data); 
	}
	
	/*客户删除*/
	public function customer_del(){
	     $cid       = I('get.cid');//单条
		 $cids      = I('post.cid');//批量
		 $cids      = join(',', $cids);
		 $customer  = M("customer");
		if (!empty($cid) && empty($cids) && isset($cid)){
		    $where['cid'] = $cid;
			$count = $customer->where($where)->delete();
		}else{
			$count = $customer->where(array('cid' => array('in', $cids)))->delete();
		}
		if($count){
		    $this->success("删除成功",U('Customer/index'));
		}else{
		    $this->error("删除失败");	
		}
	}
	
	/*客户的账号*/
	public function caccount(){
	    $caccount = M("caccount");
		import("ORG.Util.Page");// 导入分页类
		$count    = $caccount->count();
		$page     = new \Think\Page($count,10);//tp3.2新增
		$show     = $page->show();
		$limit    = $page->firstRow.','.$page->listRows;
		
		$list     = $caccount -> order("listorder desc, aid desc") 
				              -> limit($limit)->select();
		$now      = time();
		$this->assign(array(
		    'list'   =>  $list,
			'page'   =>  $show,
			'now'    =>  $now,
		));
        $this->display();
    }
	
	/*新增/修改客户账号信息*/
	public function caccount_update(){
	    $aid        =   I('get.aid');
	    $caccount   =   M("caccount"); 
		
		if (IS_POST){
			 $data['webname']      =  trim(I('post.webname','','htmlspecialchars'));
			 $data['weburl']       =  trim(I('post.weburl','','htmlspecialchars'));
			 $data['accname']      =  trim(I('post.accname','','htmlspecialchars'));
			 $data['accurl']       =  trim(I('post.accurl','','htmlspecialchars'));
			 $data['loginname']    =  trim(I('post.loginname','','htmlspecialchars'));
			 $data['loginpwd']     =  trim(I('post.loginpwd','','htmlspecialchars'));
			 $data['customer']     =  trim(I('post.customer','','htmlspecialchars'));
			 $data['contact']      =  trim(I('post.contact','','htmlspecialchars'));
			 $data['status']       =  I('post.status') ? trim(I('post.status','','htmlspecialchars')) : "1";
			 $data['content']      =  trim(I('post.content','','htmlspecialchars'));
			 $data['inputtime']    =  I("post.inpuuttime") ? strtotime(trim(I('post.inputtime','','htmlspecialchars'))) : time();
			 $data['listorder']    =  I('post.listorder') ? trim(I('post.listorder')): 0;
		    
			 if(trim(substr($data['weburl'],0,4)) <> "http" && trim(substr($data['weburl'],0,5)) <> "https"){
			    $data['weburl']   = "http://".$data['weburl'];
			 }
			  if(trim(substr($data['accurl'],0,4)) <> "http" && trim(substr($data['accurl'],0,5)) <> "https"){
			    $data['accurl']   = "http://".$data['accurl'];
			 }

		}
		
	    if (!isset($aid) || $aid ==""){//id为空表示新增
	          $count = $caccount -> add($data);
			  if($count){
				  $this->success("添加成功",U('Customer/caccount'));
			  }else{
				  $this->error("添加失败");	
			  }
		}else{//修改信息
		      $where['aid'] = $aid;
	          $count = $caccount->where($where) -> save($data);
			  if($count){
				  $this->success("修改成功",U('Customer/caccount'));
			  }else{
				  $this->error("修改失败");	
			  }
		}
	}
	
	/*客户账号资料编辑*/
	public function caccount_edit(){
	    $aid      = I('get.aid');
		$caccount = M("caccount");
		$where    = array('aid' => $aid);
		if (!empty($aid)){
		     $arr = $caccount -> where('aid = '.$aid.'') 
						      -> order('aid desc') 
						      -> find();
		}else{
		    $this->error("参数传递错误");
	    }
		 
		$this->assign(array(
		    'data' => $arr,
		));
        $this->display();
	}
	
}